package com.xyhuo.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class IsIsomorphic {
    public boolean isIsomorphic(String s, String t) {
        char[] s1 = s.toCharArray();
        char[] t1 = t.toCharArray();
        if (s1.length != t1.length) {
            return false;
        }
        Map<Character, Character> map = new HashMap<>();
        Map<Character, Character> map1 = new HashMap<>();

        int length = s1.length;
        for (int i = 0; i < length; i++) {
            if (map.containsKey(s1[i])) {
                if (!Objects.equals(map.get(s1[i]), t1[i])) {
                    return false;
                }
            } else if (map1.containsKey(t1[i])) {
                if (!Objects.equals(map1.get(t1[i]), s1[i])) {
                    return false;
                }
            } else {
                map.put(s1[i], t1[i]);
                map1.put(t1[i], s1[i]);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        IsIsomorphic i = new IsIsomorphic();
        boolean isomorphic = i.isIsomorphic("badc", "baba");
    }
}
